import { Component,OnInit} from '@angular/core';
import { Router } from '@angular/router';
// 引入Parse第三方库
import * as Parse from "parse"
(Parse as any).serverURL = "http://metapunk.cn:9999/parse"
Parse.initialize("dev")

@Component({
  selector: 'app-page-home',
  templateUrl: './page-home.component.html',
  styleUrls: ['./page-home.component.scss']
})

export class PageHomeComponent  implements OnInit {

  //获取数据库中商品信息
  goodsList:any=[
   
  ]

  isExpand:Boolean = false;
  tagList = ["推荐",  "手机",  "平板",  "电脑",  "智能穿戴",   "智能家居",    "有品精选"]
   /**
    * 关键数据：用户当前点击标签名
    */
   currentTag = 0
   /**
    * 根据点击事件，切换当前标签currentTag
    * @param tag 被点击标签名
    */
   tagClick(tag:any){
     console.log(tag)
     this.currentTag=tag
     this.loadListByCateId(tag)
   }

   async loadListByCateId(cateId:string|number){
    //获取商品信息
    let list = await this.getGoods(cateId)
    this.goodsList = list
  }

  async ngOnInit(): Promise<void> {
    this.loadListByCateId("0")

    // //获取商品信息
    // let list = await this.getGoods()
    // console.log(list)
    // // setTimeout(() => {
    //   this.goodsList = this.goodsList.concat(list)
    //   console.log( this.goodsList)      
    // // }, 1000);

    //要增加的商品信息
    let student = {goodsId:"5",goodsName:"苹果Apple iPhone 14",goodsPrice:"5399",
    goodsDetail:"支持移动联通电信5G 双卡双待手机",goodsImg:"../../assets/images/6/poster.jpg",
  goodsNumber:0,goodsCategory:1,goodsComment:"98",
detailImgPath:[
  "../../assets/images/6/detail_1.jpg",
  "../../assets/images/6/detail_2.jpg",
  "../../assets/images/6/detail_3.jpg",
]}

//创建操作
// let Goods = Parse.Object.extend("goods");
// let xiaoming = new Goods;
// let goodsJson = {goodsID:"006",goodsName:"苹果Apple iPhone 19",goodsPrice:"5399",
// goodsDetail:"支持移动联通电信5G 双卡双待手机",goodsImg:"../../assets/images/6/poster.jpg",
// goodsNumber:0,goodsCategory:"1",goodsComment:"98",detailImgPath:[
//   "../../assets/images/6/detail_1.jpg",
//   "../../assets/images/6/detail_2.jpg",
//   "../../assets/images/6/detail_3.jpg",
// ]}
// xiaoming.set(goodsJson)
// xiaoming.save();



// 查询Student
//     let query = new Parse.Query("goods")
//     query.equalTo("goodsID","006")
//     query.limit(1)
//     let slist = await query.find()
//     console.log(slist)

// 修改Student
//     let query2 = new Parse.Query("goods")
//     let stu1 = await query2.get("sVJaK06YzB");
//     stu1.set("goodsID","006")
//     stu1.save()

  // 删除Student
//     let query3 = new Parse.Query("goods")
//     let stu2 = await query3.get("SSod4F3BSl");
//     stu2.destroy()
//  {

// }
  }

  


    async getGoods(cateId:string|number){
    let result = await fetch(`http://metapunk.cn:9999/parse/classes/goods?where={"goodsCategory":"${cateId}"}`, {
      "headers": {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "if-none-match": "W/\"2b1-MU/Itc7uoKG+Snjx4wd2HA45/FA\"",
        "x-parse-application-id": "dev"
      },
      "referrer": "http://localhost:4040/",
      "referrerPolicy": "strict-origin-when-cross-origin",
      "body": null,
      "method": "GET",
      "mode": "cors",
      "credentials": "omit"
    });
    let data = await result.json();
    console.log(data)
    let list = data?.results
    return list
  }





  


  getData(url:string){
    console.log(url);
  }
  goodsImageName = ['']


/**
   * 路由跳转详情传参
   * @see
   * Router 服务，参考：https://angular.cn/tutorial/tour-of-heroes/toh-pt5
   */
goDetail(item:any) {
  console.log(item);
   this.router.navigate(["./intelligent-shopping/detail"],{
      queryParams:{
          id:item.objectId
}
   })
}
constructor(private router:Router){

}


clickInput(){
  this.router.navigate(["./intelligent-shopping/search"],{
       })
}
}
